﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;
using Infragistics.Win.UltraWinGrid;
 

namespace DMSClient
{

    public partial class Currency : Form
    {
         
        private DataTable dtCurrency;
        string ambiente;
        EndpointAddress address;
        WSHttpBinding Binding = new WSHttpBinding();
        public Currency()
        {
            InitializeComponent();             
            Binding = new WSHttpBinding();
            ambiente = Properties.Settings.Default.CurrentEnv;
            Binding.MaxReceivedMessageSize = 2147483647;
            Binding.MaxBufferPoolSize = 2147483647;
            Binding.ReceiveTimeout = TimeSpan.MaxValue;
            Binding.ReaderQuotas.MaxStringContentLength = Int32.MaxValue;
            address = new EndpointAddress(Properties.Settings.Default.Server);
            Binding.Security.Mode = SecurityMode.None;
             
            this.Text = Properties.Settings.Default.CurrentEnvDes + " - Currency";
        }

        private void Currency_Load(object sender, EventArgs e)
        {
            estado(false);
            ugCurrency.DisplayLayout.Bands[0].Columns.Add("idCurrency", "IdCurrency");
            ugCurrency.DisplayLayout.Bands[0].Columns.Add("cdCurrency", "Cod");
            ugCurrency.DisplayLayout.Bands[0].Columns.Add("dsCurrency", "Description");
            ugCurrency.DisplayLayout.Bands[0].Columns.Add("dtLastModified", "Last modified date");
            
            CurrencyWorker.RunWorkerAsync();
        }

        private void CurrencyWorker_DoWork(object sender, DoWorkEventArgs e)
        {

            DMSServices.DMSServicesClient client;
            client = new DMSServices.DMSServicesClient(Binding, address);
            string lista = "";
            try
            {
                client.Open();
               lista = client.getCurrency(ambiente);
               client.Close();

               if (CurrencyWorker.CancellationPending)
               {
                   e.Cancel = true;
                   return;
               }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex.InnerException);
            }
            if (lista == null)
            {
                throw new Exception("Error");
            }


            dtCurrency = new DataTable("Currencydt");
            dtCurrency.Columns.Add("idCurrency");
            dtCurrency.Columns.Add("cdCurrency");
            dtCurrency.Columns.Add("dsCurrency");
            dtCurrency.Columns.Add("dtLastModified");           
            DataSet ds = new DataSet();
            ds.Tables.Add(dtCurrency);

            System.IO.StringReader xmlSR = new System.IO.StringReader(lista);
            ds.ReadXml(xmlSR, XmlReadMode.ReadSchema);

            xmlSR.Close();
        }

        private void CurrencyWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
           if (e.Error != null)
            {
                if (!this.IsDisposed)
                {
                    this.BringToFront();
                    MessageBox.Show(this, e.Error.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    estado(true);
                }
            }
           else if (!e.Cancelled && !this.IsDisposed)
           {
               ugCurrency.DataSource = dtCurrency;
               loader.Visible = false;
               estado(true);
           }
        }

        public void estado(bool estado)
        {
            loader.Visible = !estado;
        }

        private void Currency_Shown(object sender, EventArgs e)
        {
            loader.Left = (this.Width - loader.Width) / 2;
            loader.Top = ((this.Height - loader.Height) / 2) - loader.Height;
        }

        private void Currency_Resize(object sender, EventArgs e)
        {
            loader.Left = (this.Width - loader.Width) / 2;
            loader.Top = ((this.Height - loader.Height) / 2) - loader.Height;
        }

        private void ultraButton1_Click(object sender, EventArgs e)
        {
            if (ugCurrency.Rows.Count < 1)
            {
                MessageBox.Show(this, "There is no data to export", "Visit Report", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            System.Windows.Forms.SaveFileDialog dialogo = new System.Windows.Forms.SaveFileDialog();
            dialogo.DefaultExt = "xls";
            Infragistics.Win.UltraWinGrid.ExcelExport.UltraGridExcelExporter Exporter = new Infragistics.Win.UltraWinGrid.ExcelExport.UltraGridExcelExporter();
            if (dialogo.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Exporter.Export(ugCurrency, dialogo.FileName);
                if (MessageBox.Show(this, "¿Desea abrir el archivo?", "Visit Report", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    System.Diagnostics.Process.Start(dialogo.FileName);
                }
            }
        }

        private void ugCurrency_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            foreach (UltraGridColumn ugc in e.Layout.Bands[0].Columns)
            {
                ugc.PerformAutoResize(PerformAutoSizeType.AllRowsInBand, true);
            }
        }

    }
}
